package com.alks.function.service.impl.chengKong;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.MinIoUtil;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.enums.chengkong.DefineEnum;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.chengKong.assort.SdStyleColorDto;
import com.alks.function.data.dto.chengKong.assort.SdStyleDto;
import com.alks.function.data.dto.chengKong.assort.SdStyleTypeDto;
import com.alks.function.data.dto.chengKong.assort.YfStyleSizeDto;
import com.alks.function.data.request.chengKong.SellStyleSaveRequest;
import com.alks.function.data.request.chengKong.StyleNoRequest;
import com.alks.function.mapper.chengKong.GeneralMaterialsMapper;
import com.alks.function.mapper.chengKong.LargeCargoTypeMapper;
import com.alks.function.service.chengKong.LargeCargoTypeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Slf4j
public class LargeCargoTypeServiceImpl implements LargeCargoTypeService {

    @Resource
    private LargeCargoTypeMapper largeCargoTypeMapper;

    @Resource
    private GeneralMaterialsMapper generalMaterialsMapper;

    /**
     * 取型体
     * @param map
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getSellStyle(Map<String, String> map) {
        if (ObjectUtils.isEmpty(map)){
            return ResponseInfo.error("参数不能为空");
        }
        String styleNo = map.get("styleNo");
        String overRide = map.get("overRide");
        if (ObjectUtils.isEmpty(styleNo)){
            return ResponseInfo.error("请输入型体!");
        }
        String materialsMapperStyleNo = generalMaterialsMapper.getStyleNo(styleNo);
        if (ObjectUtils.isEmpty(materialsMapperStyleNo)){
            return ResponseInfo.error(styleNo+",研发型体资料未建档!");
        }

        String checkUp = generalMaterialsMapper.getCheckUp(styleNo);
        if (ObjectUtils.isEmpty(checkUp)){
            return ResponseInfo.error(styleNo+",研发型体资料未检核!");
        }

        //查询是否在SD_STYLE中存在
        String companyId = UserIdThread.get().getCompanyId();
        String sdStyleNo = largeCargoTypeMapper.getStyleNo(styleNo, companyId);
        if (!ObjectUtils.isEmpty(sdStyleNo)&&"0".equals(overRide)){
            return ResponseInfo.warn("资料已存在,是否删除资料重新取数？");
        }

        if ("1".equals(overRide)){
            largeCargoTypeMapper.delSdStyle(styleNo,companyId);
            largeCargoTypeMapper.delSdStyleType(styleNo,companyId);
            delSdStyleColorPhoto(styleNo,companyId);
            delSdStyleSize(styleNo,companyId);
        }

        //查询基本资料
        YfStyle yfStyle = largeCargoTypeMapper.getStyleBasicDetail(styleNo);
        //查询制具资料
        YfStyleType yfStyleType = largeCargoTypeMapper.getStyleTypeDetail(styleNo);

        //获取用户
        String userName = UserIdThread.get().getUserName();
        //copy值
        SdStyle sdStyle = new SdStyle();
        BeanUtil.copyProperties(yfStyle,sdStyle);
        sdStyle.setCfmFlag(null);
        sdStyle.setCfmDate(null);
        sdStyle.setCfmUser(null);
        sdStyle.setSysUser(userName);
        sdStyle.setCompanyId(companyId);
        sdStyle.setSysDate(LocalDateTime.now());
        sdStyle.setDevCode(yfStyle.getCustStyle());
        sdStyle.setTechPerson(null);
        sdStyle.setBaseCutSize(null);
        sdStyle.setPrevCustStyle(null);
        sdStyle.setCustStyle(null);
        if (!ObjectUtils.isEmpty(yfStyleType)){
            sdStyle.setLastMold(yfStyleType.getLastMold());
            sdStyle.setCutTool(yfStyleType.getCutTool());
            sdStyle.setSolePart(yfStyleType.getSolePart());
        }else {
            sdStyle.setLastMold(null);
            sdStyle.setCutTool(null);
            sdStyle.setSolePart(null);
        }
        //保存到SD_STYLE
       largeCargoTypeMapper.addSdStyle(sdStyle);

        if (yfStyleType!=null){
            SdStyleType sdStyleType = new SdStyleType();
            BeanUtil.copyProperties(yfStyleType,sdStyleType);
            sdStyleType.setCompanyId(companyId);
            //保存到SD_STYLE_TYPE
            largeCargoTypeMapper.addSdStyleType(sdStyleType);
        }

        //查询型体配色资料
        List<YfStyleColorDetail> yfStyleColorDetail = largeCargoTypeMapper.getStyleColorDetail(styleNo);
        if (!ObjectUtils.isEmpty(yfStyleColorDetail)){
            List<SdStyleColorPhoto> styleColorPhotoList = yfStyleColorDetail.stream().map(s -> {
                SdStyleColorPhoto sdStyleColorPhoto = new SdStyleColorPhoto();
                BeanUtil.copyProperties(s, sdStyleColorPhoto);
                sdStyleColorPhoto.setDevCode(yfStyle.getCustStyle());
                sdStyleColorPhoto.setSysTime(LocalDateTime.now());
                sdStyleColorPhoto.setSysUser(userName);
                sdStyleColorPhoto.setCompanyId(companyId);
                return sdStyleColorPhoto;
            }).collect(Collectors.toList());

            //保存到SD_STYLE_COLOR_PHOTO
            largeCargoTypeMapper.addSdStyleColorPhoto(styleColorPhotoList);
        }

        //查询型体配码资料
        List<YfStyleSize> yfStyleSizeList = largeCargoTypeMapper.getStyleSizeDetail(styleNo);
        if (!ObjectUtils.isEmpty(yfStyleSizeList)){
            List<SdStyleSize> sdStyleSizeList = yfStyleSizeList.stream().map(s -> {
                SdStyleSize sdStyleSize = new SdStyleSize();
                BeanUtil.copyProperties(s, sdStyleSize);
                sdStyleSize.setCompanyId(companyId);
                return sdStyleSize;
            }).collect(Collectors.toList());

            //保存到SD_STYLE_SIZE
            largeCargoTypeMapper.addSdStyleSize(sdStyleSizeList);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 型体基本资料查询
     * @param request
     */
    @Override
    @AutoPageAop
    public ResponseInfo getSellStyleList(StyleNoRequest request) {
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<SdStyleDto> sdStyleDto = largeCargoTypeMapper.getSellStyleList(request);
        PageRecord<SdStyleDto> list = new PageRecord<>();
        PageInfo pageInfo = new PageInfo(sdStyleDto);
        list.setList(sdStyleDto);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 型体配色资料查询
     * @param map
     * @return
     */
    @Override
    public ResponseInfo getSellStyleColorList(Map<String, String> map) {
        String styleNo = map.get("styleNo");
        if (ObjectUtils.isEmpty(styleNo)){
            return ResponseInfo.error("型体不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        List<SdStyleColorDto> colorList = largeCargoTypeMapper.getSellStyleColorList(styleNo,companyId);
        return ResponseInfo.ok(colorList);
    }

    /**
     * 型体配码资料查询
     * @param map
     * @return
     */
    @Override
    public ResponseInfo getSellStyleSizeList(Map<String, String> map) {
        String styleNo = map.get("styleNo");
        if (ObjectUtils.isEmpty(styleNo)){
            return ResponseInfo.error("型体不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        List<YfStyleSizeDto> sizeList = largeCargoTypeMapper.getSellStyleSizeList(styleNo, companyId);
        return ResponseInfo.ok(sizeList);
    }

    /**
     * 型体制具查询
     * @param map
     * @return
     */
    @Override
    public ResponseInfo getSellStyleTypeToList(Map<String, String> map) {
        String styleNo = map.get("styleNo");
        if (ObjectUtils.isEmpty(styleNo)){
            return ResponseInfo.error("型体不能为空!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        List<SdStyleTypeDto> typeToList = largeCargoTypeMapper.getSellStyleTypeToList(styleNo,companyId);
        return ResponseInfo.ok(typeToList);
    }

    /**
     * 0检核/1取消检核
     * @param map
     * @param choose
     * @return
     */
    @Override
    public ResponseInfo getCheckOut(Map<String, String> map, Integer choose) {
        String styleNo = map.get("styleNo");
        if (ObjectUtils.isEmpty(styleNo)) {
            return ResponseInfo.error("型体不能为空!");
        }

        //判断是否检核
        String companyId = UserIdThread.get().getCompanyId();
        String flag = largeCargoTypeMapper.getSdStyleCfmFlag(styleNo,companyId);
        switch (choose) {
            case 0:
                if (!ObjectUtils.isEmpty(flag)) {
                    if (flag.equals(DefineEnum.CHECKOUT_FLAG.getValue())) {
                        return ResponseInfo.error("该型体已经检核!");
                    }
                }
                String userName = UserIdThread.get().getUserName();
                String cfmFlag = DefineEnum.CHECKOUT_FLAG.getValue();
                largeCargoTypeMapper.updateSdStyleCheckOut(styleNo, userName,companyId, cfmFlag, LocalDateTime.now());
                break;
            case 1:
                if (ObjectUtils.isEmpty(flag)) {
                    return ResponseInfo.error("该型体未检核,不能取消检核!!");
                }
                largeCargoTypeMapper.updateSdStyleCheckOut(styleNo, null,companyId, null, null);
                break;
            default:
                return ResponseInfo.error("参数错误!");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 复制
     * @param map
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getSellStyleCopy(Map<String, String> map) {
        String styleNo = map.get("styleNo");
        String newStyleNo = map.get("newStyleNo");
        if (ObjectUtils.isEmpty(styleNo)||ObjectUtils.isEmpty(newStyleNo)) {
            return ResponseInfo.error("请输入型体!");
        }
        if (newStyleNo.equals(styleNo)){
            return ResponseInfo.error("新旧型体重复相同!");
        }

        String companyId = UserIdThread.get().getCompanyId();
        String sdStyleNo = largeCargoTypeMapper.getStyleNo(newStyleNo,companyId);
        if (!ObjectUtils.isEmpty(sdStyleNo)){
            return ResponseInfo.error(newStyleNo+"大货型体资料已存在!");
        }

        //查询SD_STYLE资料
        SdStyle sdStyle = largeCargoTypeMapper.getSdStyleDetail(styleNo,companyId);
        sdStyle.setStyleNo(newStyleNo);
        largeCargoTypeMapper.addSdStyle(sdStyle);

        //查询SD_STYLE_TYPE
        SdStyleType sdStyleType = largeCargoTypeMapper.getSdStyleTypeDetail(styleNo,companyId);
        if (!ObjectUtils.isEmpty(sdStyleType)){
            sdStyleType.setStyleNo(newStyleNo);
            largeCargoTypeMapper.addSdStyleType(sdStyleType);
        }

        //查询SD_STYLE_SIZE
        List<SdStyleSize> sdStyleSizes = largeCargoTypeMapper.getSdStyleSizeDetail(styleNo,companyId);
        if (!ObjectUtils.isEmpty(sdStyleSizes)){
            for (SdStyleSize sdStyleSize : sdStyleSizes) {
                if (!ObjectUtils.isEmpty(sdStyleSize)){
                    sdStyleSize.setStyleNo(newStyleNo);
                }
            }
            largeCargoTypeMapper.addSdStyleSize(sdStyleSizes);
        }

        //查询SD_STYLE_COLOR_PHOTO
        List<SdStyleColorPhoto> sdStyleColorPhotoList = largeCargoTypeMapper.getSdStyleColorPhoto(styleNo,companyId);
        if (!ObjectUtils.isEmpty(sdStyleColorPhotoList)){
            for (SdStyleColorPhoto sdStyleColorPhoto : sdStyleColorPhotoList) {
                if (!ObjectUtils.isEmpty(sdStyleColorPhoto)){
                    sdStyleColorPhoto.setStyleNo(newStyleNo);
                }
            }
            largeCargoTypeMapper.addSdStyleColorPhoto(sdStyleColorPhotoList);
        }

        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 保存
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getSellStyleSave(SellStyleSaveRequest request) {
        if (ObjectUtils.isEmpty(request.getStyleNo())){
            return ResponseInfo.error("型体为空!");
        }
        if (!ObjectUtils.isEmpty(request.getCfmFlag())){
            if (request.getCfmFlag().equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("该型体已经检核!");
            }
        }
        if (ObjectUtils.isEmpty(request.getTypeNo())){
            return ResponseInfo.error("序号不能为空!");
        }

        String userName = UserIdThread.get().getUserName();
        String companyId = UserIdThread.get().getCompanyId();
        SdStyle sdStyle = new SdStyle();
        BeanUtil.copyProperties(request,sdStyle);
        sdStyle.setSysUser(userName);
        sdStyle.setCompanyId(companyId);
        sdStyle.setSysDate(LocalDateTime.now());

        //修改型体基本资料
        largeCargoTypeMapper.updateSdStyle(sdStyle);

        SdStyleType sdStyleType = new SdStyleType();
        sdStyleType.setCompanyId(companyId);
        sdStyleType.setStyleNo(request.getStyleNo());
        sdStyleType.setTypeNo(request.getTypeNo());
        sdStyleType.setLastMold(request.getLastMold());
        sdStyleType.setCutTool(request.getCutTool());
        sdStyleType.setSolePart(request.getSolePart());
        //修改制具资料
        largeCargoTypeMapper.updateSdStyleType(sdStyleType);

        //型体配色资料
        if (!ObjectUtils.isEmpty(request.getPhotoList())){
            List<SdStyleColorPhoto> photoList = request.getPhotoList().stream().map(s -> {
                SdStyleColorPhoto sdStyleColorPhoto = new SdStyleColorPhoto();
                BeanUtil.copyProperties(s, sdStyleColorPhoto);
                sdStyleColorPhoto.setCompanyId(companyId);
                sdStyleColorPhoto.setStyleNo(request.getStyleNo());
                sdStyleColorPhoto.setSysUser(userName);
                sdStyleColorPhoto.setSysTime(LocalDateTime.now());
                return sdStyleColorPhoto;
            }).collect(Collectors.toList());
            delSdStyleColorPhoto(request.getStyleNo(), companyId);
            largeCargoTypeMapper.addSdStyleColorPhoto(photoList);
        }else {
            delSdStyleColorPhoto(request.getStyleNo(), companyId);
        }

        //型体配码资料
        if (!ObjectUtils.isEmpty(request.getSizeList())){
            List<SdStyleSize> sdStyleSizes = request.getSizeList().stream().map(s -> {
                SdStyleSize sdStyleSize = new SdStyleSize();
                BeanUtil.copyProperties(s, sdStyleSize);
                sdStyleSize.setStyleNo(request.getStyleNo());
                sdStyleSize.setCompanyId(companyId);
                sdStyleSize.setTypeNo(request.getTypeNo());
                return sdStyleSize;
            }).collect(Collectors.toList());
            delSdStyleSize(request.getStyleNo(),companyId);
            largeCargoTypeMapper.addSdStyleSize(sdStyleSizes);
        }else {
            delSdStyleSize(request.getStyleNo(),companyId);
        }

        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 删除型体配色资料
     * @param styleNo
     * @param companyId
     */
    private void delSdStyleColorPhoto(String styleNo, String companyId) {
        largeCargoTypeMapper.delSdStyleColorPhoto(styleNo,companyId);
    }

    /**
     * 删除型体配码资料
     * @param styleNo
     */
    private void delSdStyleSize(String styleNo,String companyId) {
        largeCargoTypeMapper.delSdStyleSize(styleNo,companyId);
    }

    @Resource
    MinIoUtil minIoUtil;

    /**
     * 图片上传
     * @param file
     * @return
     */
    @Override
    public ResponseInfo updateSellStylePhoto(MultipartFile file) {
        if (null==file || 0 == file.getSize()){
            log.error("上传文件不能为空!");
            return ResponseInfo.error("上传文件不能为空!");
        }
        //获取文件后缀
        String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //生成图片名称
        String photoPath = UUID.randomUUID().toString();
        photoPath = "/photo/profile/sdstylecolor/"+photoPath+""+fileSuffix;
        //第一个参数为文件
        // 第二个参数为桶
        // 第三个是图片路径
        minIoUtil.upload(file,"photo",photoPath);
        photoPath="http://192.168.1.220:9000/photo"+photoPath;
        return ResponseInfo.ok(photoPath,"操作成功!");
    }

    /**
     * 整单删除
     * @param map
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getSellStyleDel(Map<String, String> map) {
        String styleNo = map.get("styleNo");
        if (ObjectUtils.isEmpty(styleNo)) {
            return ResponseInfo.error("请输入型体!");
        }
        //判断是否被引用
        String companyId = UserIdThread.get().getCompanyId();
        String style = largeCargoTypeMapper.getSdBomHeadStyle(styleNo,companyId);
        if (!ObjectUtils.isEmpty(style)){
            return ResponseInfo.error("型体已关联BOM资料,不能整单删除!");
        }

        //判断是否被指令引用
        Integer bomInstruct = largeCargoTypeMapper.getBomInstruct(styleNo);
        if (bomInstruct!=0){
            log.error("型体:{},已关联指令单资料,不能整单删除!",styleNo);
            return ResponseInfo.error("型体:" + styleNo  + ",已关联指令单资料,不能整单删除!");
        }
        largeCargoTypeMapper.delSdStyle(styleNo,companyId);
        largeCargoTypeMapper.delSdStyleType(styleNo,companyId);
        delSdStyleColorPhoto(styleNo,companyId);
        delSdStyleSize(styleNo,companyId);
        return ResponseInfo.ok("操作成功!");
    }

}
